package com.example.controller.other;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.common.Result;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.model.entity.other.Account;
import com.example.model.dto.AccountDto;
import com.example.service.appointment.UserService;
import com.example.service.medical.DoctorService;
import com.example.service.medical.HospitalService;
import com.example.service.other.AdminService;
import com.example.service.other.BusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 基础前端接口
 */
@RestController
@Api(tags = "登录注册接口")
public class WebController {

    @Resource
    private AdminService adminService;
    @Resource
    private UserService userService;
    @Resource
    private HospitalService hospitalService;
    @Resource
    private DoctorService doctorService;
    @Resource
    private BusinessService businessService;
    @ApiIgnore
    @GetMapping("/")
    public Result hello() {
        return Result.success("访问成功");
    }

    /**
     * 登录
     */
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody AccountDto accountDto) {
        Account account=new Account();
        if (ObjectUtil.isEmpty(accountDto.getUsername()) || ObjectUtil.isEmpty(accountDto.getPassword())
                || ObjectUtil.isEmpty(accountDto.getRole())) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        if (RoleEnum.ADMIN.name().equals(accountDto.getRole())) {
             account=adminService.login(accountDto);
        }else if (RoleEnum.USER.name().equals(accountDto.getRole())) {
            account=userService.login(accountDto);
        }else if (RoleEnum.BUSINESS.name().equals(accountDto.getRole())) {
            account=businessService.login(accountDto);
        }else if (RoleEnum.HOSPITAL.name().equals(accountDto.getRole())) {
            account=hospitalService.login(accountDto);
        }else if (RoleEnum.DOCTOR.name().equals(accountDto.getRole())) {
            account=doctorService.login(accountDto);
        }
        return Result.success(account);
    }

    /**
     * 注册
     */
    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public Result register(@RequestBody Account account) {
        if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword())
                || ObjectUtil.isEmpty(account.getRole())) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        if (RoleEnum.USER.name().equals(account.getRole())) {
            userService.register(account);
        }
        return Result.success();
    }

    /**
     * 修改密码
     */
    @ApiOperation(value = "修改密码")
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account) {
        if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword())
                || ObjectUtil.isEmpty(account.getNewPassword())) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        if (RoleEnum.ADMIN.name().equals(account.getRole())) {
            adminService.updatePassword(account);
        }else if (RoleEnum.USER.name().equals(account.getRole())) {
            userService.updatePassword(account);
        }else if (RoleEnum.BUSINESS.name().equals(account.getRole())) {
            businessService.updatePassword(account);
        }else if (RoleEnum.HOSPITAL.name().equals(account.getRole())) {
            hospitalService.updatePassword(account);
        }else if (RoleEnum.DOCTOR.name().equals(account.getRole())) {
            doctorService.updatePassword(account);
        }
        return Result.success();
    }

}
